package kov.core
{
	import com.riaoo.display.Animation;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	import kov.core.global.Global;
	
	public class WorldRenderer extends Sprite
	{
		private var world:World;
		private var mapLayer:Sprite; // 地图层
		private var bodyLayer:Sprite; // Body 层
		private var staticLayer:Sprite; // 前景层
		
		/**
		 * 构造函数。
		 */		
		public function WorldRenderer(world:World)
		{
			super();
			
			this.world = world;
			this.mapLayer = new Sprite();
			this.bodyLayer = new Sprite();
			this.staticLayer = new Sprite();
			
			addChild(this.mapLayer);
			addChild(this.bodyLayer);
			addChild(this.staticLayer);
			
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}
		
		/**
		 * 添加地图。
		 * @param content
		 */		
		public function addMap(content:DisplayObject):void
		{
			this.mapLayer.addChild(content);
		}
		
		/**
		 * 添加 Body 。
		 * @param body
		 */		
		public function addBody(body:DisplayObject):void
		{
			this.bodyLayer.addChild(body);
			adjustBodyLayer();
		}
		
		/**
		 * 添加一个装饰物。
		 * @param content
		 */		
		public function addStatic(content:DisplayObject):void
		{
			this.staticLayer.addChild(content);
		}
		
		/**
		 * 将游戏可视焦点（中心点）定位到指定的点。
		 * @param x
		 * @param y
		 */		
		public function locateAt(x:Number, y:Number):void
		{
			var left:Number = Global.GAME_WIDTH / 2;
			var right:Number = this.world.mapData.width - Global.GAME_WIDTH / 2;
			var top:Number = Global.GAME_HEIGHT / 2;
			var bottom:Number = this.world.mapData.height - Global.GAME_HEIGHT / 2;
			if (x < left)
			{
				x = left;
			}
			else if (x > right)
			{
				x = right;
			}
			
			if (y < top)
			{
				y = top;
			}
			else if (y > bottom)
			{
				y = bottom;
			}
			
			var rect:Rectangle = this.scrollRect;
			rect.x = x - rect.width / 2;
			rect.y = y - rect.height / 2;
			this.scrollRect = rect;
		}
		
		/**
		 * 游戏可视焦点跟踪 player 。
		 */		
		public function doLocateAtPlayer():void
		{
			Global.stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		/**
		 * 消除游戏可视焦点跟踪 player 。
		 */		
		public function undoLocateAtPlayer():void
		{
			Global.stage.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		// 居中于舞台。
		private function center():void
		{
			var rect:Rectangle = this.scrollRect;
			this.x = (this.stage.stageWidth - rect.width) / 2;
			this.y = (this.stage.stageHeight - rect.height) / 2;
		}
		
		// 调整 Body 层的深度排序。
		private function adjustBodyLayer():void
		{
			var bodys:Array = [];
			var d:DisplayObject;
			var l:uint = this.bodyLayer.numChildren
			for (var i:int = 0; i < l; i++)
			{
				bodys.push(this.bodyLayer.getChildAt(i));
			}
			bodys.sortOn("y", Array.NUMERIC);
			for each (d in bodys)
			{
				this.bodyLayer.addChild(d);
			}
		}
		
		
		
		//---------- 事件处理 ----------
		private function onAddedToStage(event:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			this.stage.addEventListener(Event.RESIZE, onStageResize);
			this.scrollRect = new Rectangle(0, 0, Global.GAME_WIDTH, Global.GAME_HEIGHT);
			center();
		}
		
		private function onStageResize(event:Event):void
		{
			center();
		}
		
		private function onEnterFrame(event:Event):void
		{
			locateAt(this.world.player.x, this.world.player.y - this.world.player.visibleCube.depth);
			adjustBodyLayer();
		}
		
	}
}